package client;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;

import org.omg.CORBA.ORB;

import libraryCORBA.libraryCommand;
import libraryCORBA.libraryCommandHelper;

class LibraryClient extends CommandClient {
	
	private libraryCommand library;
	
	
	private static String[] institutionIdentifiers = new String[]{"CONCORDIA", "MCGILL", "UQAM"};
	private static String[] institutionNames = new String[]{"Concordia", "McGill", "UQAM"};
	
	private String username;
	private String password;
	
	public LibraryClient(libraryCommand library) {
		super();
		this.library = library;
	}
	
	
	public void run() {
		while(true) {
			int option = mainMenu();
			
			switch (option) {
			case 1:
				createAccountMenu();
				break;
				
			case 2:
				reserveBookMenu();
				break;
				
			case 3:
				reserveInterLibraryMenu();
				break;
				
			case 4:
				setDurationMenu();
				break;
			case 5:
				getNonReturnersMenu();
				break;
				
			case 6:
				System.out.println("Exiting DRMS.");
				System.exit(0);
			}
		}
	}
	
	public void promptCredentials() {
		printTitle("Enter Credentials");
		username = prompt("Username: ");
		password = prompt("Password: ");
	}
	
	public String promptInstitution() {
		int institutionIndex = promptMenu("Educational Institution", "Select your institution", institutionNames);
		return institutionIdentifiers[institutionIndex - 1];
	}
	
	public void printResponse(String response) {
		System.out.println("DRMS command output:");
		System.out.println(response);
		waitForEnter();
	}
	
	public void createAccountMenu() {
		printTitle("Create Account");
		
		String firstName = prompt("First Name");
		String lastName = prompt("Last Name");
		String username = prompt("Username");
		String password = prompt("Password");
		String phone = prompt("Phone Number");
		String email = prompt("Email Address");

		String institution = promptInstitution();
		
		System.out.println("Creating Account...");
		String response = library.createAccount(firstName, lastName, email, phone, username, password, institution);
	
		printResponse(response);
	}
	
	
	public void reserveBookMenu() {
		printTitle("Reserve Book");
		
		promptCredentials();
		
		String title = prompt("Book Title");
		String author = prompt("Book Author");
		
		String institution = promptInstitution();
		
		System.out.println("Reserving Book...");
		
		String response = library.reserveBook(username, password, title, author, institution);
		
		printResponse(response);
		
	}
	
	public void reserveInterLibraryMenu() {
		printTitle("Reserve Inter-Library");

		promptCredentials();
		
		String bookName = prompt("Book Name");
		String authorName = prompt("Author Name");
		String institution = promptInstitution();
		
		System.out.println("Reserving inter-library...");
		
		String response = library.reserveInterLibrary(username, password, bookName, authorName, institution);

		printResponse(response);
		
	}
	
	public void setDurationMenu() {
		printTitle("Set Duration");
		
		String username = prompt("Username");
		String bookName = prompt("Book name");
		int numDays = promptInt("Number of days");
		String institution = promptInstitution();

		System.out.println("Sending set duration command...");
		
		String response = library.setDuration(numDays, username, bookName, institution);
		
		printResponse(response);
		
	}
	
	public void getNonReturnersMenu() {
		printTitle("Get Non-Returners");
		
		promptCredentials();
		
		int numDays = promptInt("Number of days");
		String institution = promptInstitution();
		
		String response = library.getNonReturners(username, password, numDays, institution);
		
		printResponse(response);
		
	}
	
	public int mainMenu() {
		showHeader("Distributed Reservation Management System");
		return promptMenu("Main Menu", "Select an option", 
				"Create Account",
				"Reserve Book",
				"Reserve Inter-Library",
				"Get Non-Returners",
				"Set Duration",
				"Exit");
	}
	

	
	public static void main(String[] args) throws IOException {
	
		libraryCommand library = RemoteLibraryHelper.createRemoteLibraryInstance();
	
		LibraryClient client = new LibraryClient(library);
		
		client.run();
	}
}